//
//  NewsController.m
//  dfdsf
//
//  Created by dllo on 16/1/24.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "NewsController.h"
#import "NewsOfCell.h"
#import "CellForHotNews.h"
#import "NewsOfCell.h"
#import "CellForAds.h"
#import "RecommendOfCell.h"
#import "NBAOfCell.h"
#import "InteballOfCell.h"
#import "ChineseBallOfCell.h"
#import "CBAOfCell.h"
#import "ModelForHotNews.h"
#import "WebviewForNews.h"
#import "ModelForRecommend.h"
#import "ModelForNBA.h"
#import "SearchController.h"
#import <DKNightVersion.h>
#import "ImageController.h"
#import "AVPlayControllerViewController.h"
#import "UIColor+Wonderful.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

#define WEIGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667


@interface NewsController ()<UICollectionViewDataSource, UICollectionViewDelegate, NewsOfCellDelegate, NBAOfCellDelegate, InteballOfCellDelegate, ChineseBallOfCellDelegate, CBAOfCellDelegate>

@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) UIView *captin;
@property (nonatomic, retain) UIView *viewForWhiteLine;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) UISegmentedControl *segment;
@property (nonatomic, retain) NSMutableArray *arrayForHot;
@property (nonatomic, retain) NSIndexPath *indexPath;


@end

@implementation NewsController

-(void)dealloc {
    
     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"id" object:nil];
     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"disappear" object:nil];
    [_collection release];
    [_captin release];
    [_viewForWhiteLine release];
    [_array release];
    [_segment release];
    [_arrayForHot release];
    [_indexPath release];
    
    [super dealloc];

}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];

    self.segment.hidden = NO;


}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
   //[self handleHotData];
    
    
    self.navigationController.navigationBar.tintColor = [UIColor redColor];
    [self createCaptin];
    [self createCollection];
    [self createKVO];
    [self createNotification];
    
    
    
//    // 点击推荐通过通知中心传值到下一页面
//    
//    [[NSNotificationCenter defaultCenter] addObserverForName:@"id" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
//           WebviewForNews *webViewNews = [[WebviewForNews alloc] init];
//        
//        ModelForRecommend *model = [note.userInfo objectForKey:@"ID"];
//        
//        webViewNews.url = model.url;
//        
//        [self.navigationController pushViewController:webViewNews animated:YES];
//        
//        
//        [webViewNews release];
//        
//    }];
    
    
}
- (void)createNotification {
    
    // 点击推荐通过通知中心传值到下一页面
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"id" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        WebviewForNews *webViewNews = [[WebviewForNews alloc] init];
        
        ModelForRecommend *model = [note.userInfo objectForKey:@"ID"];
        
        webViewNews.url = model.url;
        
        [self.navigationController pushViewController:webViewNews animated:YES];
        
        
        [webViewNews release];
        
    }];

    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"disappear" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
       
        self.segment.hidden = YES;
        
    }];
    
    
}


#pragma mark - 创建头部标题
- (void)createCaptin {

    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorWithColors([UIColor deepSkyBlue], [UIColor lightGrayColor]);

    
    self.viewForWhiteLine = [[UIView alloc] initWithFrame:CGRectMake(0, 42, WIDTH / 6, 2)];
    [self.navigationController.navigationBar addSubview:self.viewForWhiteLine];
    [_viewForWhiteLine release];
    self.viewForWhiteLine.backgroundColor = [UIColor whiteColor];
    
    self.segment = [[UISegmentedControl alloc] initWithItems:@[@"热门", @"推荐", @"NBA", @"国际足球", @"中国足球", @"CBA"]];
   self.segment.frame = CGRectMake(0, 0, WIDTH, 40);
    [self.navigationController.navigationBar addSubview:self.segment];
    [_segment release];
    self.segment.layer.cornerRadius = 0;
    self.segment.layer.masksToBounds = YES;
    
    //去掉颜色,现在整个segment都看不见
    self.segment.tintColor = [UIColor clearColor];
    self.segment.selectedSegmentIndex = 0;
    
       // 设置选中时候的 segment 的属性
    NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName: [UIColor whiteColor]};
    [self.segment setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];
    
    // segment 没有被选中的时候的属性
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:[UIColor lightTextColor]};
    [self.segment setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    
    [self.segment addTarget:self  action:@selector(itemClick:) forControlEvents:UIControlEventValueChanged];

}

- (void)itemClick:(UISegmentedControl *)seg {

    self.collection.contentOffset = CGPointMake(seg.selectedSegmentIndex * WIDTH, - 64);
    
}

#pragma mark - 创建 cellectionView 

- (void)createCollection {

   UICollectionViewFlowLayout *flayout = [[UICollectionViewFlowLayout alloc] init];
    flayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flayout.itemSize = CGSizeMake(WIDTH, HEIGHT - 113);

    flayout.minimumInteritemSpacing = 0;
    flayout.minimumLineSpacing = 0;
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flayout];
    [flayout release];
    [self.view addSubview:self.collection];
    self.collection.pagingEnabled = YES;
    self.collection.dk_backgroundColorPicker = DKColorWithRGB (0xffffff, 0x343434);
    [_collection release];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.collection.showsHorizontalScrollIndicator = NO;
    
  //注册不同的 collectionViewCell
    [self.collection registerClass:[NewsOfCell class] forCellWithReuseIdentifier:@"poolForCollection"];
    
    [self.collection registerClass:[RecommendOfCell class] forCellWithReuseIdentifier:@"poolForRecommend"];
    
    [self.collection registerClass:[NBAOfCell class] forCellWithReuseIdentifier:@"poolForNBA"];
    
    [self.collection registerClass:[InteballOfCell class] forCellWithReuseIdentifier:@"poolForInte"];
    
    [self.collection registerClass:[ChineseBallOfCell class] forCellWithReuseIdentifier:@"poolForChieseBall"];
    
    [self.collection registerClass:[CBAOfCell class] forCellWithReuseIdentifier:@"poolForCBA"];
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 6;

}

//**为 collectionViewCell 赋值 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.item == 0) {
        
        NewsOfCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForCollection" forIndexPath:indexPath];
        
        cell.delegate = self;
        
        
         return cell;
        
        
    }else if (indexPath.item == 1) {
    
        RecommendOfCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForRecommend" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
        
   
        return cell;
    
    
    }else if (indexPath.item == 2) {
    
        NBAOfCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForNBA" forIndexPath:indexPath];
        
        cell.delegate = self;
        [cell.button addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
        
        return cell;
    }else if (indexPath.item == 3){
    
        InteballOfCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForInte" forIndexPath:indexPath];
        cell.delegate = self;
        [cell.button addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];

    
        return cell;
    }else if (indexPath.item == 4) {
    
        ChineseBallOfCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForChieseBall" forIndexPath:indexPath];
        cell.delegate = self;
        [cell.button addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];

        return cell;
        
    } else {
    
        CBAOfCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForCBA" forIndexPath:indexPath];
        cell.delegate = self;
        [cell.button addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];

        return cell;

    }

    
}

- (void)nextAction:(UIButton *) button {

    self.segment.hidden = YES;
    SearchController *search = [[SearchController alloc] init];
    
    [self.navigationController pushViewController:search animated:YES];
    
    [search release];

    
}



#pragma mark - 协议方法跳转下一页面

/** 点击热门进入下一页面的代理方法 */
-(void)didClickNewsRow:(ModelForHotNews *)model {
    
    
    if (model.img_data != nil) {
        
        ImageController *ImageVC = [[ImageController alloc] init];
        NSString *str = @"http://platform.sina.com.cn/sports_client/slide?app_key=2586208540&_version__=3.2.1.01&__os__=android&format=json&sid=";
        NSString *str2 = @"&album_id=";
        NSString *str3 = @"&size=img&num=300&client_deviceid=e1626cc2f63146d9302c32f415dfedf0";
        ImageVC.url = [str stringByAppendingFormat:@"%@%@%@%@", model.sid, str2, model.album_id, str3];
        // NSLog(@"%@", ImageVC.url);
        ImageVC.edgesForExtendedLayout = UIRectEdgeNone;
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:ImageVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
        [ImageVC release];
        
        
    } else if (model.short_title != nil) {
        
        // 视频播放器
        
        AVPlayControllerViewController *av = [[AVPlayControllerViewController alloc] init];
        
        NSString *str = @"http://v.iask.com/v_play_ipad.php?vid=";
        
        NSString *url = [model.video_info objectForKey:@"ipad_vid"];
        av.flashUrl = [str stringByAppendingFormat:@"%@", url];
        
        [self.navigationController pushViewController:av animated:NO];
        
        [av release];
        
        
    } else {
        
        WebviewForNews *webViewNews = [[WebviewForNews alloc] init];

        webViewNews.url = model.url;
        
        // NSLog(@"%@", model.url);
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:webViewNews animated:YES];
        self.hidesBottomBarWhenPushed = NO;
        [webViewNews release];
    }
    
}

/** 点击NBA进入下一页面 */
- (void)didClickNBARow:(ModelForNBA *)model {

    WebviewForNews *webViewNews = [[WebviewForNews alloc] init];
    
    webViewNews.url = model.url;
    
    [self.navigationController pushViewController:webViewNews animated:YES];
    
    [webViewNews release];

}
/** 点击国际足球进入下一页面 */
- (void)didClickInteballRow:(ModelForNBA *)model {

    WebviewForNews *webViewNews = [[WebviewForNews alloc] init];
    
    webViewNews.url = model.url;
    
    [self.navigationController pushViewController:webViewNews animated:YES];
    
    [webViewNews release];

}

/**点击中国足球进入下一页面 */
-(void)didClickChineseBallRow:(ModelForNBA *)model {

    WebviewForNews *webViewNews = [[WebviewForNews alloc] init];
    
    webViewNews.url = model.url;
    
    [self.navigationController pushViewController:webViewNews animated:YES];
    
    [webViewNews release];

}

/**点击 CBA 进入下一个页面 */
- (void)didClickCBARow:(ModelForNBA *)model {

    WebviewForNews *webViewNews = [[WebviewForNews alloc] init];
    
    webViewNews.url = model.url;
    
    [self.navigationController pushViewController:webViewNews animated:YES];
    
    [webViewNews release];


}



#pragma - KVO

- (void)createKVO {

    [self.collection addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];

}
/**当观察者偏移量发生变化调用此方法 */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {

//    self.viewForWhiteLine.frame = CGRectMake(self.segment.selectedSegmentIndex * WIDTH / 6, 42, WIDTH / 6, 2);

    CGFloat X = [[change objectForKey:@"new"] CGPointValue].x;
    
    CGFloat whiteX = X / 6;
    
    self.segment.selectedSegmentIndex = X / WIDTH;
    
    self.viewForWhiteLine.transform = CGAffineTransformMakeTranslation(whiteX, 0);

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
